package 反射机制详解.反射的使用;

import java.lang.reflect.*;

/**
 * @program: algorithm
 * @description:
 * @author: 陈恩涛
 * @create: 2021-12-27 14:43
 **/
public class TestReflection {

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        System.out.println(String.class.getCanonicalName());
        System.out.println(String.class.getName());
        System.out.println(String.class.getSimpleName());
        Constructor<?>[] constructors = String.class.getDeclaredConstructors();
        for (Constructor<?> constructor : constructors) {
            for (Type type : constructor.getGenericParameterTypes()) {
                System.out.println(type);
            }

            System.out.println("=============");
        }
        Student student = new Student();
        Field age = student.getClass().getDeclaredField("age");
        age.setInt(student,15);
        System.out.println(student);
        Method setName = student.getClass().getMethod("setName", String.class);
        setName.invoke(student,"张三");
        System.out.println(student);
        System.out.println(setName.getName());
        System.out.println(setName.toGenericString());
    }
}
